/*
 * @Author: 一盏青灯欲锁魂
 * @Date: 2021-06-17 14:40:02
 * @LastEditTime: 2021-06-17 14:58:55
 * @Description: http 异常过滤器
 * @FilePath: \nest_demo\src\filter\http_exception.filter.ts
 */
import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from '@nestjs/common';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter<HttpException>{
    catch(exception:HttpException, host:ArgumentsHost){
        const _ctx = host.switchToHttp(),
            _response = _ctx.getResponse(),
            _request = _ctx.getRequest();
        const _status = exception.getStatus(),
            _exceptionRes:any = exception.getResponse();
        const { error, message } = _exceptionRes;

        _response.status(_status).json({
            status:_status,
            timestamp: new Date().toISOString(),
            path: _request.url,
            error,
            message,
        })
    }
}